#include "core/GameObject.h"
#include <iostream>
#include <stdlib.h>

using namespace eats_core;
using namespace eats_vars;
using namespace std;

GameObject::GameObject(SDL_Surface * _workingSurf, int _posX, int _posY)
{
	_id = "";
	_canonical_name = "";
	if(_workingSurf == NULL)
	{
		cout << "|WARNING| Não foi passado a surpeficie de destino" << endl;
		_workingSurf = new SDL_Surface;
	} else {
		workingSurf = _workingSurf;
	}

	//FIXME GRANDE DRAGAO BRANCO DAS INICIALIZACOES NO LUGAR ERRADO
	position = new Point;
	size = new Size;

	position->x = _posX;
	position->y = _posY;

	_isAlive = true;

}

GameObject::~GameObject()
{
	//dtor
}

bool GameObject::render()
{
	if( !workingSurf )
	{
		return false;
	}

	bool ret = true;
	if( componentEnabledList.size() == 0)
	{
		ret = false;
	}
	for( unsigned int i = 0; i < componentEnabledList.size() ; i ++) {
		if( ( componentEnabledList.at(i) )-> render() == false )
		{
			cout << "|ERRO| Não foi possível desenhar o objeto ["<< i <<"]" << endl;
			ret = false;
		}
	}
	return ret;
}

bool GameObject::update()
{
	bool ret = true;
	if( componentEnabledList.size() == 0)
	{
		ret = false;
	}
	for( unsigned int i = 0; i < componentEnabledList.size() ; i ++) {
		if( ( componentEnabledList.at(i) )->update() == false )
		{
			ret = false;
		}
	}
	return ret;
}

bool GameObject::event()
{

}

bool GameObject::setUp()
{
	bool ret = true;
	if( componentList.size() == 0)
	{
		ret = false;
	}
	cout << "|INFO| Iniciando carregamento de componentes..." <<endl;
	for( unsigned int i = 0; i < componentList.size() ; i ++) {
		if( ( componentList.at(i) )-> setUp() == false )
		{
			cout << "|WARNING| Não foi possível carregar o componente ["<< i <<"] ["<< ( componentList.at(i) )->_canonical_name <<"]" << endl;
			ret = false;
		} else {
			cout << "|INFO| Carregamento efetuado com sucesso, componente ["<< i <<"]" << endl;
		}
	}
	cout << "|INFO| Carregamento de componentes finalizado, resultado final (1 = Sucesso, 0 = Falha) : "<< ret << ""<<endl;
	return ret;
}

bool GameObject::tearDown()
{
	bool ret = true;
	if( componentList.size() == 0)
	{
		return ret;
	}
	for( unsigned int i = 0; i < componentList.size() ; i ++) {
		if(componentList.at(i) != NULL){
			( componentList.at(i) )->tearDown();
			delete componentList.at(i);
		}
	}

	delete position;
	delete size;

	return ret;
}


void GameObject::addComponent(GameComponent * comp)
{
	componentList.push_back(comp);
}

bool GameObject::enableComponent ( string id )
{
	for(unsigned int i =0; i < componentList.size(); i++) {
		if( componentList.at(i)->_id.compare(id ) == 0 ){
			componentEnabledList.push_back(componentList.at(i));
			return true;
		}
	}
	return false;
}
bool GameObject::enableComponent ( int position )
{
	if( position > componentList.size() || position < 0 )
		return false;
	componentEnabledList.push_back(componentList.at(position));
	return true;
}
bool GameObject::enableComponent ( GameComponent * ref )
{
	for(unsigned int i =0; i < componentList.size(); i++) {
		if( componentList.at(i) == ref ){
			componentEnabledList.push_back(componentList.at(i));
			return true;
		}
	}
	return false;
}
bool GameObject::disableComponent ( string id )
{
	vector<GameComponent *>::iterator iter = componentEnabledList.begin();
	for( int i = 0;	iter != componentEnabledList.end(); iter++, i++) {
		if( componentEnabledList.at(i)->_id.compare(id) == 0 ){
			componentEnabledList.erase(iter);
			return true;
		}

	}
	return false;
}
bool GameObject::disableComponent ( unsigned int position )
{
	if( position > componentList.size() || position < 0 )
		return false;
	componentEnabledList.erase(componentEnabledList.begin() + position);
	return true;
}

bool GameObject::disableComponent ( GameComponent * ref )
{
	vector<GameComponent *>::iterator iter = componentEnabledList.begin();
	for( int i = 0;	iter != componentEnabledList.end(); iter++, i++) {
		if( componentEnabledList.at(i) == ref ){
			componentEnabledList.erase(iter);
			return true;
		}

	}
	return false;
}

